home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n03.arc / HELLOPMW.C < prev    next >
Text File  |  1991-01-17  |  2KB  |  73 lines

  1.  
  2. /*
  3.     HELLOPMW.C  ---  "Hello Protected Mode World!"
  4.     Illustrates use of the DPMI interface to switch into
  5.     protected mode, and use of Windows 3's built-in DOS
  6.     Extender to print a message in protected mode.
  7.  
  8.     Compile in SMALL MODEL with:  CL HELLOPMW.C
  9.  
  10.     Execute under Windows 3.0 only!
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. unsigned modeswitch();
  16.  
  17. main() 
  18. {
  19.     unsigned saveCS, saveDS;
  20.  
  21.     _asm mov saveCS,cs          ; store real mode CS 
  22.     _asm mov saveDS,ds          ; and DS for display
  23.  
  24.     printf("\nHello, real mode world! \tCS=%04xh DS=%04xh", 
  25.         saveCS, saveDS);
  26.  
  27.     if(modeswitch())            // attempt mode switch
  28.     {
  29.         puts("\nDPMI not available or memory allocation failed.");
  30.         exit(1);
  31.     }
  32.  
  33.     _asm mov saveCS,cs          ; store protected mode CS
  34.     _asm mov saveDS,ds          ; and DS for display
  35.  
  36.     printf("\nHello, protected mode world! \tCS=%04xh DS=%04xh\n", 
  37.         saveCS, saveDS);
  38.  
  39.     _asm mov ah,4ch             ; exit directly to DOS to avoid
  40.     _asm int 21h                ; GP fault in RTL cleanup code
  41. }
  42.  
  43. /*
  44.     Call DPMI to switch from real mode to protected mode.
  45.     Returns false if mode switch successful, true if no DPMI 
  46.     present or if memory allocation for DPMI data area failed.
  47. */
  48. unsigned modeswitch(void)
  49. {
  50.     void far *switchpoint;              // far pointer to DPMI
  51.                                         // mode switch entry point
  52.     _asm
  53.     {
  54.         mov     ax,1687h                ; get DPMI mode switch
  55.         int     2fh                     ; entry point...
  56.         or      ax,ax                   ; bail out if no DPMI
  57.         jnz     exitlabel
  58.         mov     word ptr switchpoint,di ; save entry point
  59.         mov     word ptr switchpoint+2,es
  60.         mov     bx,si                   ; allocate DPMI private
  61.         mov     ah,48h                  ; data area
  62.         int     21h
  63.         jc      exitlabel               ; jump, allocation failed
  64.         mov     es,ax                   ; pass segment of data area     
  65.         mov     ax,0                    ; indicate we are 16-bit app
  66.         call    switchpoint             ; switch to protected mode
  67.         mov     ax,0                    ; signal success
  68.     }
  69.  
  70.     exitlabel: ;                        // any "no return value" warning 
  71. }                                       // message here can be ignored
  72.  
  73.